var AjaxCrud = QualityCheck.AjaxCRUD;
var serverUrl = QualityCheck.GPUD.ServiceConfig;
$(function(){
	initPages();
	window.onresize = function () {
		initPages();
	};
	pageBtns();
	getUserInfo();
});
function initPages(){
	var winH = document.documentElement.clientHeight || document.body.clientHeight;
	var bodyH = winH - $(".index-head").height() - $(".footer").height()-5;
	$(".index-body").height(bodyH);
}
function getUserInfo(){
	var seachUrl = decodeURI(window.location.search);
	var paramStr = seachUrl.substring(1);
	params = paramStr.split("&");
	var parm = {};
	for(var i = 0; i < params.length; i++){
		var key = params[i].split("=")[0];
		var value = params[i].split("=")[1];
		parm[key] = value;
	}
	for(var key in parm){
		if(parm[key] == "" || parm[key] == undefined || parm[key] == null){
			window.location.href = encodeURI("login.html");
			return false;
			break;
		}
	}
	$("#user-name").text(substr(parm.username,4)).attr("title",parm.username).attr("key",parm.userkey).attr("code",parm.usercode).attr("usertype",0).attr("schoolcode",parm.schoolcode).attr("rolecodes",parm.rolecodes);
	$("#user-head").attr("src","../"+ ServiceName + parm.headimg);
	//getLicense(parm.usercode,0,parm.userkey);
	getModels(parm.usercode,0,parm.userkey);
}
function getModels(usercode,usertype,userKey){
	var data = {
			cmd:"queryUserlicenseModel",
			model:"license",
			userKey:userKey,
			data:{
				usercode: usercode,
				usertype: usertype
			},
//			cmd:"queryModels",
//			model:"license",
//			userKey:userKey,
//			data:{
//			},
			remark:"",
	};
	AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
		if(res.data != "" && res.result == 1){
			addMainModels(res.data);
		}else{
			processError("数据加载失败！");
		}
	});
}
function getLicense(usercode,usertype,userKey){
	var data = {
			cmd:"queryAdminRole",
			model:"roles",
			userKey:userKey,
			data:{
				usercode:usercode,
				usertype:usertype
			},
			remark:"",
	};
	AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
		if(res.data != "" && res.result == 1){
			var models = $(".head-menu");
			var list = res.data[0].roles.split(",");
			if(list.length > 0 && list[0] != ""){
				for(var i = 0; i < models.length; i++){
					for(var j = 0; j < list.length; j++){
						if(models[i].getAttribute("model") == list[j]){
							models[i].style.display = "block";//显示具有权限的模块
							if(j == 0){
								models[i].className = "head-menu head-menu-select";//给有权限的第一个模块添加选中样式
							}
						}
					}
				}
				//默认进入的模块
				if(list[0] == 1){
					changePage(PageUrl.aschoolManageUrl);//校务
				}else if(list[0] == 2){
					changePage(PageUrl.apositionIndexUrl);//位置
				}else if(list[0] == 3){
					changePage(PageUrl.anewsManageUrl);//新闻
				}else if(list[0] == 4){
					changePage(PageUrl.userManageUrl);//用户
				}else if(list[0] == 5){
					changePage(PageUrl.aterminalManageUrl);//终端
				}
			}else{
				alertBox("warn","请联系超级管理员进行授权",false);
			}
		}else{
			alertBox("warn","请联系超级管理员进行授权",false);
		}
	});
}
function substr(str,len){
	if(str != undefined){
		if(str.length > len){
			str = str.substring(0,len-1)+"...";
			return str;
		}else{
			return str;
		}
	}else{
		return "";
	}
}
function pageBtns(){
//	$(".head-menu").click(function(){
//		$(".head-menu").removeClass("head-menu-select");
//		if($(this).text() == "新闻公告"){
//			$(this).addClass("head-menu-select");
//			changePage(PageUrl.anewsManageUrl);
//		}else if($(this).text() == "位置服务"){
//			$(this).addClass("head-menu-select");
//			changePage(PageUrl.apositionIndexUrl);
//		}else if($(this).text() == "校务管理"){
//			$(this).addClass("head-menu-select");
//			changePage(PageUrl.aschoolManageUrl);
//		}else if($(this).text() == "权限管理"){
//			$(this).addClass("head-menu-select");
//			changePage(PageUrl.userManageUrl);
//		}else if($(this).text() == "终端设置"){
//			$(this).addClass("head-menu-select");
//			changePage(PageUrl.aterminalManageUrl);
//		}
//	});
	
	$(".logout-btns").click(function(){
		window.location.href = encodeURI("login.html");
	});
	
	$(".person-set-btns").click(function(){
		var usercode = $("#user-name").attr("code");
		var userKey = $("#user-name").attr("key");
		var usertype = $("#user-name").attr("usertype");
		var url = "setting.html?usercode="+usercode+"&userkey="+userKey+"&usertype="+usertype;
		window.open(url);
	});
	
	$(".user-info").mouseover(function(){
		$("#navigate").show();
	});
	
	$(".sys-menu").mouseover(function(){
		$("#navigate").hide();
	});
	
	$(".index-logo").mouseover(function(){
		$("#navigate").hide();
	});

	$(".index-body").mouseover(function(){
		$("#navigate").hide();
	});
	
	$(".logout-btns").mouseover(function(){
		$(".logout-btns").addClass("logout-btns-select");
	});
	
	$(".logout-btns").mouseout(function(){
		$(".logout-btns").removeClass("logout-btns-select");
	});
	
	$(".person-set-btns").mouseover(function(){
		$(".person-set-btns").addClass("person-set-btns-select");
	});
	
	$(".person-set-btns").mouseout(function(){
		$(".person-set-btns").removeClass("person-set-btns-select");
	});
	
	
	$(".user-info").mouseout(function(){
		$("#navigate").mouseover(function(){
			$("#navigate").show();
		});
		
		$("#navigate").mouseout(function(){
			$("#navigate").hide();
		});
	});
	$(".room-map-close").click(function(){
		$(".index-dialog-cover").hide();
		$(".room-map-dialog").hide();
	});
}
function changePage(pageurl){
	$("#main-body").attr("src",pageurl);
}



